package com.wyzz.hr.util.comparator;

import java.io.PrintStream;
import java.util.Comparator;

import com.wyzz.hr.util.reflect.ObjectProperty;

public class ObjectFieldComparator extends IComparator implements Comparator {

	public ObjectFieldComparator() {
	}

	public ObjectFieldComparator(Class objClass, String fieldList[],
			int compareMethod[]) throws Exception {
		initial(objClass, fieldList, compareMethod);
	}

	public ObjectFieldComparator(Class objClass, String fieldList[],
			int orderList[], int compareMethod[]) throws Exception {
		initial(objClass, fieldList, orderList);
	}

	public void initial(Class objClass, String fieldList[], int compareMethod[])
			throws Exception {
		int orderList[] = new int[fieldList.length];
		for (int i = 0; i < fieldList.length; i++)
			orderList[i] = 1;

		initial(objClass, fieldList, orderList, compareMethod);
	}

	public void initial(Class objClass, String fieldList[], int orderList[],
			int compareMethod[]) throws Exception {
		if (fieldList == null || orderList == null || fieldList.length < 1
				|| fieldList.length != orderList.length) {
			System.out
					.println("==========error: the format of fieldList and orderList is not valid");
			throw new Exception();
		}
		objectProperty = new ObjectProperty[fieldList.length];
		objComparator = new ObjectComparator[fieldList.length];
		for (int i = 0; i < fieldList.length; i++) {
			objectProperty[i] = new ObjectProperty(objClass, fieldList[i]);
			objComparator[i] = new ObjectComparator(orderList[i], new int[0]);
		}

		this.fieldList = fieldList;
		this.orderList = orderList;
		if (compareMethod.length > 0)
			setCompareMethod(compareMethod[0]);
	}

	public int compare(Object obj1, Object obj2) {
		for (int i = 0; i < fieldList.length; i++) {
			int resultTmp = objComparator[i].compare(objectProperty[i]
					.getProperty(obj1), objectProperty[i].getProperty(obj2));
			if (resultTmp != 0)
				return resultTmp;
		}

		return 0;
	}

	private String fieldList[];
	private int orderList[];
	private ObjectProperty objectProperty[];
	private ObjectComparator objComparator[];
}
